package elcs.group.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import elcs.board.model.ListVO;
import elcs.board.model.PagingBean;
import elcs.member.model.MemberDao;
import elcs.member.model.MemberVO;

@Repository
public class GroupServiceImpl implements GroupService{

	@Resource(name="groupDaoImpl")
	private GroupDao groupDao;
	
	@Resource(name="memberDaoImpl")
	private MemberDao memberDao;
	
	@Transactional
	@Override
	public void createGroup(GroupVO gvo,HttpSession session) {
		// TODO Auto-generated method stub
		
		MemberVO mvo =(MemberVO)session.getAttribute("mvo");
		gvo.setGleader(mvo.getId());
		groupDao.createGroup(gvo);
		mvo.setGvo(groupDao.findByGname(gvo.getGname()));
		memberDao.updateMemberByGroup(mvo);
		session.setAttribute("mvo", mvo);
		
		
	}

	@Override
	public List<GroupVO> mypageMyGroup() {
		// TODO Auto-generated method stub
		return groupDao.mypageMyGroup();
	}

	@Override
	public void groupJoinMember(GroupMemberVO gmvo) {
		// TODO Auto-generated method stub
		groupDao.groupJoinMember(gmvo);
	}

	@Override
	public List findRankList() {
		// TODO Auto-generated method stub
		return groupDao.groupRankList();
	}
	@Override
	public boolean checkGroupByName(String gname) {
		// TODO Auto-generated method stub
		GroupVO gvo=groupDao.findByGname(gname);
		System.out.println(gvo);
		if(gvo==null){
			return true;
		}
		return false;
	}

	@Override
	public ListVO findAllGroup(HttpServletRequest request) {
		// TODO Auto-generated method stub
			String pageNo=request.getParameter("pageNo");
			if(pageNo==null||pageNo=="") 
				pageNo="1";
			List list=groupDao.findAllGroup(pageNo);
			int total=groupDao.totalContent();
			PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
			ListVO lvo=new ListVO(list,paging);
			return lvo;
		
	}

	@Override
	public Map findGroupByName(String name) {
		// TODO Auto-generated method stub
		Map map=new HashMap();
		GroupVO gvo=groupDao.findByGname(name);
		List list= memberDao.findmemberByGroup(name);
		gvo.setAmount(list.size());
		map.put("groupinfo",gvo);
		map.put("list",list);
		
		return map;
	}



}
